package worker

import (
	"NovelBook/logic/book"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetBorrowedInfoByBookID(c *gin.Context) {
	id_any, exit := c.Get(auth.WorkerIDKey)
	id := id_any.(uint)
	if !exit || id < 0 {
		response.Fail(c, http.StatusBadRequest, response.CodeTokenInvalid)
		return
	}

	BookID := c.Query("id")
	if len(BookID) == 0 {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}
	ID, err := strconv.Atoi(BookID)
	if err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	BorrowInfo, err := book.GetBorrowedInfoByBookID(ID)
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, err.Error())
		return
	}
	response.Success(c, BorrowInfo)
}
